Home and Learn: VB Net Course


Coding the Plus Button

Let's remind ourselves how our calculator from the previous section works works. To add up 5 + 9, we'd do this:

  1. Click first on the 5
  2. A 5 appear in the textbox
  3. Click the + symbol
  4. The 5 disappears from the textbox
  5. Click on the number 9
  6. A 9 appears in the textbox
  7. Click on the = symbol
  8. The 9 disappears from the textbox
  9. The answer to 5 + 9 appears in the textbox
  10. Click the "Clear" button to clear the textbox

We've done numbers 1 and 2 on that list. We're now going to do numbers 3 and 4 on the list. What we're trying to do is this: Click on the Plus symbol and make the number in the Textbox disappear. Before the number vanishes, we can store it in a variable. The variable we're going to be storing the number in is one of those variable we set up at the top of the code. It's this one:

Dim total1 As Integer

We've already seen how to retain a value from a textbox and add it to something else:

txtDisplay.Text = txtDisplay.Text & btnZero.Text

Here, we kept the value that was already in the textbox and joined it to the Text property of the button.

We can do something similar if we want to retain a value that is already in a variable. Examine this:

variable1 = variable1 + 1

The "= variable1 + 1" part just says "Remember what is in the variable variable1, and then add 1 to it. So if variable1 contain the number 3, what would variable1 now hold after that bit of code is executed? The whole code might be this:

variable1 = 3
variable1 = variable1 + 1

(If you don't know the answer to that, please send an email and ask for some further clarification on the subject.)

The above is known in programming terms as "Incrementing a variable". There is even a shorthand you can use:

variable1 += 1

This says "variable1 equals variable1 plus 1". Or "Add 1 to variable1". You can also use the other mathematical operators with the same shorthand notation:

variable1 = 10
variable1 *= 3

This new code says "Multiply whatever is inside of variable1 by 3".

The shorthand notation can be tricky to read (and to get used to), so we won't use it much. But it's there is you want it.

Retaining VB NET Values

Back to our code.

If we're going to be adding two numbers together, we need Visual Basic to remember the first number. Because when we click the equals sign, that first number will be added to the second number. So if we haven't asked VB to remember the first number, it won't be able to add up.

The variable we're going to be storing the first number in is total1. We could just say this:

total1 = txtDisplay.Text

Then whatever is in the textbox will get stored in the variable total1.

Except we want VB to remember our numbers. Because we might want to add three or more numbers together: 1 + 2 + 3 + 4. If we don't keep a running total, there's no way for our programme to remember what has gone before, it will just erase whatever is in total1 and then start again.

So just like the code above (varaible1 = variable1 + 1), we need to "tell" our programme to remember what was in the variable. We do it like this:

total1 = total1 + Val(txtDisplay.Text)

That Val( ) part just makes sure that a number in a textbox is kept as a number, and not as text. It's short for Value. The important part is the total1 + txtDisplay.Text. We're saying "The variable total1 contains whatever is in total1 added to the number in the textbox." An example might clear things up. Suppose we were adding 5 + 9. Now, suppose total1 already contained the number 5, and that number 9 was in the textbox. It would work like this:

Finally, we need to erase the number in the textbox. To erase something from a textbox, just set its Text property to a blank string. We do this with two double quotation marks. Like this:

txtDisplay.Text = ""

That tiny bit of code will erase the Text a textbox. Another way to erase text from a textbox is to use the Clear method. After you typed a full stop, you probably saw the drop down list of Properties and Methods. Scroll up to the top, and locate the word Clear. Double click "Clear" and the drop down list will close. Hit the return key and VB adds two round brackets to your code:

txtDisplay.Clear( )

Notice that we're not setting the textbox to equal anything. We're using something called a Method. You can tell it's a Method because there's a purple block icon next to the word. A Method is a built-in bit of code that VB knows how to execute. In other words, it knows how to clear text from a textbox. You'll learn more about Methods later.

So the whole code for our Button called btnPlus is this:

Private Sub btnPlus_Click(sender As Object, e As EventArgs) _
Handles btnPlus.Click

total1 = total1 + Val(txtDisplay.Text)

txtDisplay.Clear()

End Sub

Add that code to your Plus button. All we've done with that code is to store numbers into our variable total1 and then erase whatever was in the textbox.

We now need to add the numbers up.

Exercise F

Write the code for the equals button. There's only three lines in total, and here's a little help.

You need to use the other variable that was set up at the top of the coding window, total2. The variable total1 will be added to whatever is total2

The first line of code will be this

total2 = total1 + (something missing here)

Your job is to supply the missing code. In other words, replace "(something missing here)"

Remember that total1 contains the first number to be added. And you know where that came from. The only thing left to do is to add the second number.

For the second line of code, you need to transfer the total2 variable to the textbox.

For the third line of code, all you are doing is resetting the variable total1 to zero. That's because after the equals button has been pressed, we have finished adding up. If you wanted to do some more adding up, that total1 will still hold the value from the last time. If you reset it to zero, you can start adding some new numbers.

The only thing left to do is to write a single line of code for the Clear button. All you are doing there is erasing text from a textbox. Which you already know how to do.

Answer to Exercise F

 

When you're finished, you should have a simple calculator that can add up integers. In the next section, we'll take a look at the extra things you can do with a VB NET message box.

Back to the VB NET Contents Page

 


Buy the Book of this Course

Email us: enquiry at homeandlearn.co.uk